import {ApiCode, ApiResult, Method, Page, RequestUrlEnum} from "@/common-class/common-config/ApiClass";
import {request} from "@/apis/request";
import store from '@/store/index';
import {LoginWind3User, Wind3User} from "@/common-class/model/Wind3User";
import {ElMessage} from "element-plus";
import router from "@/router";
import {RouteNameEnum} from "@/common-class/common-config/RouteClass";
import {Wind3Test} from "@/common-class/model/Wind3Test";

/**
 * 统一的请求方法
 * @param urlName 请求地址，只支持enum
 * @param config 参数配置
 */
function apiSend<T>(urlName: RequestUrlEnum, config: { data?: any, params?: any, method: Method }) : Promise<ApiResult<T>> {
  return new Promise((resolve, reject) => {
    const req = request({
      method: config.method,
      url: urlName.toString(),
      data: config.data,
      params: config.params,
    });
    req.then(res => {
      if ((res.headers['content-type']).includes('application/json')) {
        const data = res.data as ApiResult<T>
        if (data.apiCode === ApiCode.Success) {
          resolve(data)
        } else if (data.apiCode === ApiCode.TokenExpired || data.apiCode === ApiCode.NotLogin) {
          const message: string = data.apiCode === ApiCode.TokenExpired ? 'token已过期，请重新登录' : '您还未登录系统';
          ElMessage({duration: 1000, type: 'error', message});
          router.replace({name: RouteNameEnum.login}).then((r) => {
            store.commit('user/setToken', null);
          });
        } else {
          ElMessage({duration: 1000, type: 'error', message: data.msg});
          resolve(data)
        }
      }
      resolve(res.data)
    }).catch(reason => {
      const apiResult = new ApiResult<T>()
      apiResult.msg = reason
      apiResult.apiCode = ApiCode.Fail
      apiResult.page = null
      resolve(apiResult)
    })
  })
}

/**
 * 获取全部用户列表
 * @param data 查询参数
 */
function getWind3UserList(data: Wind3User | Page) : Promise<ApiResult<Wind3User[]>> {
  return apiSend<Wind3User[]>(RequestUrlEnum.userGetList, { params: data, method: 'get'})
}

/**
 * 用户登录功能
 * @param data 登录系统的用户参数
 */
function login(data: Wind3User) : Promise<ApiResult<LoginWind3User>> {
  return apiSend<LoginWind3User>(RequestUrlEnum.userLogin, { data: data, method: 'post'}, )
}

/**
 * 获取登录系统的用户信息
 */
function getLoginUserInfo() : Promise<ApiResult<LoginWind3User>> {
  return apiSend<LoginWind3User>(RequestUrlEnum.userLoginInfo, { method: 'get' } )
}

/**
 * 用户系统登出
 */
function logout() : Promise<ApiResult<boolean>> {
  return apiSend<boolean>(RequestUrlEnum.userLogout, { method: 'get' })
}

/**
 * 获取测试对象列表
 * @param data 查询的数据
 */
function getTestList(data: Wind3Test | Page) : Promise<ApiResult<Wind3Test[]>> {
  return apiSend<Wind3Test[]>(RequestUrlEnum.testGetList, {method: 'get', params: data})
}

/**
 * 插入一条测试对象
 * @param data 要插入的测试对象
 */
function insertTest(data: Wind3Test): Promise<ApiResult<boolean>> {
  return apiSend<boolean>(RequestUrlEnum.testInsert, {method: 'post',  data})
}

export const apiMethod = {
  /** 用户部分相关接口 */
  getWind3UserList, // 获取用户列表
  login, // 登录系统
  getLoginUserInfo, // 获取登录用户的信息
  logout, // 退出系统

  /** 测试对象部分接口 */
  getTestList,
  insertTest
}





